﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;

using Infrastructure.NetExtensions;

namespace TempEmployees.Web
{
    public partial class DisplayDropList : BaseDataDisplay
    {
        public delegate void FillDropDownListDelegate(DropDownList list);

        public EventHandler OnSelectedIndexChanged
        {
            set { DispDropDownList.SelectedIndexChanged += new EventHandler(value); }
        }

        public override bool ReadOnly
        {
            set
            {
                DispLabel.Visible = value;
                DispDropDownList.Visible = !value;
                if (value) DispLabel.Text = DispDropDownList.SelectedItem.Text;
            }
            get
            {
                return DispLabel.Visible;
            }
        }

        public override bool AutoPostBack
        {
            get { return DispDropDownList.AutoPostBack; }
            set { DispDropDownList.AutoPostBack = value; }
        }

        public SelectedValueObject SelectedValue
        {
            get 
            {
                return DispDropDownList.SelectedValue; 
            }
            set 
            { 
                DispDropDownList.SelectedValue = value;
 
            }
        }

        public string Text
        {
            get { return (IsOptionalValueSelected) ? TextBoxOptional.Text : DispDropDownList.SelectedItem.Text; }
        }

        public int SelectedIndex
        {
            get { return DispDropDownList.SelectedIndex; }
            set { DispDropDownList.SelectedIndex = value; }
        }

        public ListItemCollection Items
        {
            get { return DispDropDownList.Items; }
        }

        protected override WebControl EditableControl
        {
            get { return DispDropDownList; }
        }

        private const string OPTIONAL_INDEX = "optional_index";
        public SelectedValueObject OptionalValue
        {
            set
            {
                ViewState.Set(OPTIONAL_INDEX, value);
            }
            get
            {
                return ViewState.Get<SelectedValueObject>(OPTIONAL_INDEX);
            }
        }

        protected bool IsOptionalValueSelected
        {
            get { return (OptionalValue != null && OptionalValue == DispDropDownList.SelectedValue); }
        }

        [CategoryAttribute("Appearance"),
         DescriptionAttribute("Extra info shown next to the text box"),
         DefaultValueAttribute("")]
        public string OptionalLabelText
        {
            get { return LabelOptional.Text; }
            set { LabelOptional.Text = value; }
        }

        public void SetDropDownList(FillDropDownListDelegate fillMethod)
        {
            fillMethod(DispDropDownList);
            if (DispDropDownList.Items.Count > 0) DispDropDownList.SelectedIndex = 0;
            SetDisplayState();
        }

        public override void Reset()
        {
            if (DispDropDownList.Items.Count > 0) DispDropDownList.SelectedIndex = 0;
            DispLabel.Text = string.Empty;
        }

        public override bool IsFilled()
        {
            return (SelectedIndex >= 0 && 
                ((IsOptionalValueSelected && TextBoxOptional.Text.Length > 0) || !IsOptionalValueSelected));
        }

        public override bool IsValid()
        {
            return true;
        }

        protected void DispDropList_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetDisplayState();
        }

        private void SetDisplayState()
        {
            PanelTextBox.Visible = IsOptionalValueSelected;
        }
    }
}